﻿using ServiceWeb.DBGateway;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsBase;
using Microsoft.EntityFrameworkCore;
using UtilsORM.MySqlData;
using System.Reflection.Emit;
using Models.Common;
using System.Web;
using System.Xml.Linq;
using UtilsORM;
using UtilsCache;

namespace ServiceWeb.WebAdmin
{
    public class ServiceIotDeviceModelFile
    {
        EFYihongIotContext ef = new EFYihongIotContext();

        public void loadInfoFileTemp(string guid)
        {
            List<ModelFileInfo> getfiles = new List<ModelFileInfo>();
            List<iot_device_model_file> files=ef.iot_device_model_file.Where(c=>c.f_m_guid==guid && c.f_type=="file").ToList();
            foreach(iot_device_model_file file in files)
            {
                ModelFileInfo newfile = new ModelFileInfo();
                newfile.name = file.f_filename; ;
                newfile.url = file.f_url;
                getfiles.Add(newfile);
            }
            UtilMemoryCacheHelper.set<List<ModelFileInfo>>(guid, getfiles);
        }
        public void saveInfoFileTemp(string guid, string name, string fileurl)
        {
            List<ModelFileInfo> getfiles = UtilMemoryCacheHelper.get<List<ModelFileInfo>>(guid);
            if (getfiles == null)
            {
                getfiles = new List<ModelFileInfo>();
            }
            ModelFileInfo newfile = new ModelFileInfo();
            newfile.name = name;
            newfile.url = fileurl;
            getfiles.Add(newfile);

            UtilMemoryCacheHelper.set<List<ModelFileInfo>>(guid, getfiles);
        }

        public List<ModelFileInfo> getInfoFileTemp(string guid)
        {
            List<ModelFileInfo> getfiles = UtilMemoryCacheHelper.get<List<ModelFileInfo>>(guid);
            if (getfiles == null)
            {
                getfiles = new List<ModelFileInfo>();
            }
            return getfiles;
        }

        public List<ModelFileInfo> delInfoFileTemp(string guid, string fileurl)
        {
            List<ModelFileInfo> getfiles = UtilMemoryCacheHelper.get<List<ModelFileInfo>>(guid);
            if (getfiles == null)
            {
                getfiles = new List<ModelFileInfo>();
            }
            ModelFileInfo delfile = getfiles.Where(c => c.url == fileurl).FirstOrDefault();
            if (delfile != null)
            {
                getfiles.Remove(delfile);
                UtilMemoryCacheHelper.set<List<ModelFileInfo>>(guid, getfiles);
            }
            return getfiles;
        }

    }

}
